package com.iot.iotplus.mqtt;

import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.stereotype.Component;

@Log4j2
@Component
public class SendCallBack implements MqttCallback {
    /**
     * 断开连接
     * @param throwable
     */
    @Override
    public void connectionLost(Throwable throwable) {
        log.info("【消息发送客户端与mqtt远程服务器断开】");
    }

    /**
     * 接收到消息
     * @param s
     * @param mqttMessage
     * @throws Exception
     */
    @Override
    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
        log.info("接收到消息");
    }

    /**
     * 发送完成
     * @param iMqttDeliveryToken
     */
    @SneakyThrows
    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        MqttMessage message = iMqttDeliveryToken.getMessage();
        log.info("【发送消息id：】"+message.getId());
        log.info("【发送消息qos：】"+message.getQos());
        log.info("【发送消息topic：】"+ new String(message.getPayload()));
    }
}
